from manim import *
class math(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(60*2+1):       
            dot1=Dot([-500,5*(i-60),0]).set_opacity(0.5)
            dot2=Dot([500,5*(i-60),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(100*2+1):        
            dot3=Dot([(i-100)*5,-300,0]).set_opacity(0.5)
            dot4=Dot([(i-100)*5,300,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果 
        self.camera.frame.save_state()
        #题目出现
        text0 = Tex(r"\text{(2023)}",color=YELLOW
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-37.5,2.5,0])
        text = Tex(r"\text{设函数}$f\left( x\right) $\text{在}$\left[ -a,a\right] $\text{上有二阶连续导数,证明：}"
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-7,2.5,0])
       
        

        text1 = Tex(r"\text{(I)若}$f\left( 0\right) = 0,$\text{则存在}$\xi \in \left( -a,a\right) ,$\text{使得}$f''\left( \xi \right) =\dfrac{1}{a^{2}}\left[ f\left( a\right) +f\left( -a\right) \right] $"
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-10,-2.5,0])
        text2 = Tex(r"\text{(II)若}$f\left( x\right)$\text{在}$\left( -a,a\right) $\text{内取到极值,则存在}"
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-26,-7.5,0])
        text20 = Tex(r"$\eta \in \left( -a,a\right) $\text{使得}$\left| f''\left( \eta \right) \right| \geq \dfrac{1}{2a^{2}}\left| f\left( a\right) -f\left( -a\right) \right| $"
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([23,-7.5,0])
        self.play(Write(text0),Write(text),Write(text1),Write(text2),Write(text20),self.camera.frame.animate.scale(0.8))
        self.wait()
        #镜头拉回
          #分析文本
        #text100=MarkupText("<b><i>分析/解：</i></b>",color=PINK).scale(5.0).move_to([-38,12.5,0])
        self.play(Restore(self.camera.frame))
        self.play(FadeIn(dot1),run_time=0.1)
        self.play(FadeIn(dot2),run_time=0.1)
        self.play(FadeIn(dot3),run_time=0.1)
        #题目向上移动
        self.play(text0.animate().move_to([-37.5,22.5,0]),text.animate().move_to([-7,22.5,0]),text1.animate().move_to([-10,17.5,0]) ,text2.animate().move_to([-26,12.5,0]) ,text20.animate().move_to([23,12.5,0])   )

        rectangle=Rectangle(color=BLUE,fill_opacity=0.3,width=33,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([-28, 12.5, 0])
        self.play(Create(rectangle))

        text3 = Tex(r"\text{设}$f\left( x\right)$\text{在}$x=x_0$\text{取到极值}"
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-28,5,0])
        self.play(Write(text3))

        text4 = Tex(r"\text{费马定理}",color=YELLOW
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,5,0])
        self.play(Write(text4))
        text5 = Tex(r"$f'\left( x_{0}\right) =0$",color=YELLOW
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,5,0])
        
        self.play(ReplacementTransform(text4,text5))
        self.wait(0.2)
        text6 = Tex(r"$f\left( a\right) =f\left( x_{0}\right) +f'\left( x_{0}\right) \left( a-x_{0}\right) +\dfrac{f''\left( \eta_1 \right) }{2}\left( a-x_{0}\right) ^{2}$",color=WHITE
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-2,0])
        text7 = Tex(r"$f\left( -a\right) =f\left( x_{0}\right) +f'\left( x_{0}\right) \left( -a-x_{0}\right) +\dfrac{f''\left( \eta_2 \right) }{2}\left( -a-x_{0}\right) ^{2}$",color=WHITE
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])
        self.play(Write(text6),Write(text7))

        rectangle1=Rectangle(color=BLUE,fill_opacity=0.3,width=17,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle1.move_to([-2, -2, 0])
        rectangle2=Rectangle(color=BLUE,fill_opacity=0.3,width=19,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle2.move_to([-2, -10, 0])
        self.play(Create(rectangle1),Create(rectangle2))
        text60 = Tex(r"$f\left( a\right) =f\left( x_{0}\right) +\dfrac{f''\left( \eta_1 \right) }{2}\left( a-x_{0}\right) ^{2}$",color=WHITE
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-2,0])
        text70 = Tex(r"$f\left( -a\right) =f\left( x_{0}\right) +\dfrac{f''\left( \eta_2 \right) }{2}\left( -a-x_{0}\right) ^{2}$",color=WHITE
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])
        self.play(ReplacementTransform(text6,text60),ReplacementTransform(text7,text70),FadeOut(rectangle1,rectangle2))
        self.wait(0.2)

        rectangle3=Rectangle(color=YELLOW,fill_opacity=0.3,width=18,height=5
                            ,stroke_color=YELLOW,stroke_width=20)
        rectangle3.move_to([40, 12.5, 0])
        self.play(Create(rectangle3))

        text8 = Tex(r"$f\left( a\right) -f\left( -a\right) =\dfrac{f''\left( \eta_1 \right) }{2}\left( a-x_{0}\right) ^{2}-\dfrac{f''\left( \eta _{2}\right) }{2}\left( a+x_{0}\right) ^{2}$",color=WHITE
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-5,0])
        self.play(ReplacementTransform(text60,text8),ReplacementTransform(text70,text8),FadeOut(rectangle3))
        self.wait(0.2)
        text9 = Tex(r"$\left| f\left( a\right) -f\left( -a\right)\right| =\left|\dfrac{f''\left( \eta_1 \right) }{2}\left( a-x_{0}\right) ^{2}-\dfrac{f''\left( \eta _{2}\right) }{2}\left( a+x_{0}\right) ^{2}\right|$",color=WHITE
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-5,0])
        self.play(ReplacementTransform(text8,text9))
        self.wait(0.2)
        
        #下划线
        dot1=Dot([-10,-9,0])
        dot2=Dot([31,-9,0])
        line=Line(dot1,dot2).set_color(GREEN)
        line.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line))
        text10 = Tex(r"$\left| a\pm b\right| \leq \left| a\right| +\left| b\right| $",color=YELLOW
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(line,DOWN,buff=1)
        self.play(Write(text10))
        text11 = Tex(r"$\leq\left| \dfrac{f''\left( \eta_1 \right) }{2}\left( a-x_{0}\right) ^{2}\right| +\left| \dfrac{f''\left( \eta _{2}\right) }{2}\left( a+x_{0}\right) ^{2}\right| $",color=WHITE
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(line,DOWN,buff=1)
        self.play(ReplacementTransform(text10,text11),FadeOut(line))
        self.wait(0.2)
        rectangle4=Rectangle(color=BLUE,fill_opacity=0.3,width=8,height=6
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle4.move_to([-5, -13, 0])
        rectangle5=Rectangle(color=BLUE,fill_opacity=0.3,width=8,height=6
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle5.move_to([19, -13, 0])
        self.play(Create(rectangle4),Create(rectangle5),FadeOut(text5,text3))
        text12 = Tex(r"\text{令}$M=\max \left\{ \left| f''\left( \eta _{1}\right) \right| ,\left| f''\left( \eta _{2}\right) \right| \right\} $",color=YELLOW
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20,5,0])
        self.play(Write(text12))
        text13 = Tex(r"\text{则有}$\left| f''\left( \eta_1\right) \right| \leq M,\left| f''\left( \eta _{2}\right) \right| \leq M$",color=YELLOW
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([20,5,0])
        self.play(Write(text13))

        text14 = Tex(r"$\leq \dfrac{M}{2}\left| \left( a-x_{0}\right) ^{2}\right| +\dfrac{M}{2}\left| \left( a+x_{0}\right) ^{2}\right| $",color=WHITE
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([5,-20,0])
        self.play(ReplacementTransform(text13,text14),FadeOut(rectangle4,rectangle5))
        self.wait(0.2)
        text15 = Tex(r"$\leq \dfrac{M}{2}\left( \left| \left( a-x_{0}\right) ^{2}\right| +\left| \left( a+x_{0}\right) ^{2}\right| \right)  $",color=WHITE
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([5,-20,0])
        self.play(ReplacementTransform(text14,text15))
        self.wait(0.2)
        text16 = Tex(r"$\leq \dfrac{M}{2}\left[ \left( a-x_{0}\right) ^{2}+\left( a+x_{0}\right) ^{2}\right]  $",color=WHITE
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([3,-20,0])
        self.play(ReplacementTransform(text15,text16))
        self.wait(0.2)
        text17 = Tex(r"$\leq \dfrac{M}{2}\left( 2a^{2}+2x_{0}^{2}\right)  $",color=WHITE
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-3,-20,0])
        self.play(ReplacementTransform(text16,text17))
        self.wait(0.2)
        text18 = Tex(r"\text{因为}$-a <x_{0} <a $",color=YELLOW
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([20,5,0])
        self.play(Write(text18))
        text19 = Tex(r"\text{所以}$x_{0}^{2} <a^{2}$",color=YELLOW
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([20,5,0])
        self.play(ReplacementTransform(text18,text19))
        self.wait(0.2)
        text20 = Tex(r"$\leq 2Ma^{2}$",color=YELLOW
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text17,RIGHT,buff=1)
        self.play(ReplacementTransform(text19,text20))
        self.wait(0.2)
        text21 = Tex(r"\text{若}$M=\left| f''\left( \eta_1\right) \right| $",color=GREEN
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-40,-14,0])
        self.play(Write(text21))
        text22 = Tex(r"\text{取}$\eta=\eta _{1}$",color=RED
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-24,-14,0])
        self.play(Write(text22))
        text23 = Tex(r"\text{若}$M=\left| f''\left( \eta_2\right) \right| $",color=GREEN
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-40,-20,0])
        self.play(Write(text23))
        text24 = Tex(r"\text{取}$\eta=\eta _{2}$",color=RED
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-24,-20,0])
        self.play(Write(text24))
        text25 = Tex(r"$=2\cdot \left| f'\left( \eta \right) \right| \cdot a^{2}$",color=YELLOW
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text20,RIGHT,buff=1)
        rectangle=Rectangle(color=GREEN,fill_opacity=0.3,width=34,height=5
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle.move_to([32.5, 12.5, 0])
        

        self.play(ReplacementTransform(text24,text25),ReplacementTransform(text22,text25),Create(rectangle))
        self.wait(0.2)

        self.wait()